package com.lanswon.qzsmk.controller.cardManageController;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.base.HttpContants;
import com.lanswon.qzsmk.service.cardManageService.LossAndUnbindService;
import com.lanswon.qzsmk.vo.LossCardInfo;
import com.lanswon.qzsmk.vo.ResultVo;
import com.lanswon.qzsmk.vo.VoucherVo;

/**
 * 解除挂失controller
 * @author guyawei
 *
 */
@Controller
@RequestMapping(value="/relieveLoss")
public class RelieveLossController  {

	@Autowired
	LossAndUnbindService lbs;
	
	
	/**
	 * 跳转解挂查询页面
	 * @return
	 */
	@RequiresPermissions("relieveLoss:index")
	@RequestMapping(value="/queryUnlossCard",name="解挂,relieveLoss:index",method={RequestMethod.GET,RequestMethod.POST})
	public String queryUnlossCard(){
		return "UnlossCard";
		
	}
	
	/**
	 * 获取解挂信息
	 * @param lci
	 * @param pageCount
	 */
	@RequiresPermissions("relieveLoss:getUnloosCardInfo")
	@RequestMapping(value="/getUnloosCardInfo",name="解挂查询解挂信息,relieveLoss:getUnloosCardInfo",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<LossCardInfo> getUnloosCardInfo(LossCardInfo lci,Integer pageNum,Integer numPerPage){
//		if(lci.getNumPerPage()==null){//页面初次加载
//			lci.setCurrentPage(1);
//			lci.setNumPerPage(20);
//		}else {//页面分页
//			if(lci.getCurrentPage()<=1||pageCount==0){
//				lci.setCurrentPage(1);
//			}else if(lci.getCurrentPage()>=pageCount&&pageCount!=0){
//				lci.setCurrentPage(pageCount);
//			}
//		}
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;	
		}
		lci.setCurrentPage(pageNum);
		lci.setNumPerPage(numPerPage);
		lci.setStartLine((lci.getCurrentPage()-1)*lci.getNumPerPage());
		DwzPage<LossCardInfo> dPage= lbs.queryUnbindCard(lci);
			
		return dPage;
	}
	
	/**
	 * 处理跳转解挂登记页面的参数
	 * @param unLossCard
	 * @param request
	 * @return
	 */
	@RequiresPermissions("relieveLoss:goUnloosCardrRegister")
	@RequestMapping(value="/goUnloosCardrRegister",name="解挂处理跳转解挂登记页面的参数,relieveLoss:goUnloosCardrRegister",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> goUnloosCardrRegister(String[] unLossCard,HttpServletRequest request){
		for(String str:unLossCard){
			System.out.println(str);
		}
		LossCardInfo lci=new LossCardInfo();
		lci.setCitizenCardNo(unLossCard[0]);
		lci.setCardPName(unLossCard[1]);
		lci.setPaperTypeName(unLossCard[2]);
		lci.setPaperNo(unLossCard[3]);
		lci.setCustomerName(unLossCard[4]);
		lci.setCardStatus(unLossCard[5]);
		request.getSession().setAttribute("lci", lci);
		Map<String, Object> map=new HashMap<>();
		map.put("statusCode", "200");
		map.put("callbackType", "forward");
		map.put("forwardUrl", "relieveLoss/unLossRegister");//post请求
		return map;
	}
	
	/**
	 * 链接解挂登记页面
	 * @param request
	 * @return
	 */
	@RequiresPermissions("relieveLoss:unLossRegister")
	@RequestMapping(value="/unLossRegister",name="解挂登记页面,relieveLoss:unLossRegister",method=RequestMethod.POST)
	public String unLossRegister(HttpServletRequest request){
		
		return "UnlossRegister";
	}
	
	/**
	 * 获取session中的解挂内容
	 * @return
	 */
	@RequiresPermissions("relieveLoss:getUnlossInfo")
	@RequestMapping(value="/getUnlossInfo",name="解挂获取session中的解挂数据,relieveLoss:getUnlossInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getUnlossInfo(HttpServletRequest request){
		LossCardInfo lci=(LossCardInfo) request.getSession().getAttribute("lci");
		request.getSession().removeAttribute("lci");
		Map<String, Object> map=new HashMap<>();
		map.put("lci", lci);
		return map;
	}
	
	
	/**
	 * 提交解挂信息
	 * @param lci
	 * @return
	 * @throws Exception 
	 */
	@RequiresPermissions("relieveLoss:commitUnloosCardInfo")
	@RequestMapping(value="/commitUnloosCardInfo",name="解挂提交,relieveLoss:commitUnloosCardInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> commitUnloosCardInfo(LossCardInfo lci,HttpServletRequest request) throws Exception{
		VoucherVo  vvo=null;
		ResultVo resultVo=lbs.commitUnbindCardInfo(lci);
		Map<String, Object> map=new HashMap<>();
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			vvo=(VoucherVo) resultVo.getServiceObject();
			//map.put("statusCode", "200");
			//map.put("callbackType", "forward");
			//map.put("forwardUrl", "relieveLoss/queryUnlossCard");//post请求
			request.getSession().setAttribute("vo", vvo);
			request.getSession().setAttribute("type", "lossCancel");
			map.put("max", "true");
	        map.put("title", "解挂凭证");
	        map.put("rel", "relieve");
	        map.put("statusCode", "200");
	        map.put("url", "printCredentials/toHairpinPdfView");
	        //20181204 hgl 以下三行
	        map.put("jumpToTabid", "unLoosCard");
	        map.put("jumpToUrl", "relieveLoss/queryUnlossCard");
	        map.put("jumpToTile", "解挂");
		}else{
			map.put("statusCode", "300");
			map.put("message", resultVo.getResultStatus().getMessage());
		}
		return map;
	}
}
